window.ROOT = "";
(function() {
	
    var primaryNode=[];
    var url = window.rootPath +'/ui/ueditor/getBussinessLevelAllNodes';
    jQuery.ajax({
        url:url,
        cache:false,
        type :"get",
        dataType:"json",
        contentType:'application/json;charset=utf-8',//解决返回数据中的中文乱码问题
        success:function(data){
                primaryNode.push(
                    {
                        id: 0,
                        name: "商户区域",
                        open: "true",
                    	nocheck: "true"
                        // iconSkin:"diy03"
                    }
                );
                for (var i = 0; i < data.nodes.length; i++) {
                    primaryNode.push(data.nodes[i]);
                }
            $.fn.zTree.init($("#bussinessLevelTree"),modalSetting,primaryNode);
        }
    });
	
	
	var serverData = {};
    var serverTable;
    
    var serverData2 = {};
    var serverTable2;
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/bussinessLevelBinding/getbaseinfopage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                data: null
                },
                {
                    data: 'id',
                    render: function (data, type, full, meta) {
                        var operate ='<input type="checkbox" infoid="'+data+'" group="unbindingCB"/>';
                        return operate;

                    }
                },
                {
                    data: 'shopName',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'shopAdress',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'arreaManagementPerson.name',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'signPerson'
                },
                {
                    data: 'managementPerson.name'
                }
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();
                    if (target.className.indexOf('fa-download') != -1 || target.className.indexOf('downloadBtnE') != -1) {
                    	downloadQ(data);
                    }
                    
                    if (target.className.indexOf('fa-search') != -1 || target.className.indexOf('searchBtn') != -1) {
                        showDetail(data);
                    }

                    return;
                });
            }
        });
        
        serverData2.bussinessLevelId = jQuery('#bussinessLevelId').val();;
        serverTable2 = jQuery('#dataTable2').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/bussinessLevelBinding/getbaseinfobylevelpage',
                "data": function (d) {
                    return $.extend({}, d, serverData2);
                }
            },
            columns: [
                {
                data: null
                },
                {
                    data: 'id',
                    render: function (data, type, full, meta) {
                        var operate ='<input type="checkbox" infoid="'+data+'" group="bindingCB"/>';
                        return operate;

                    }
                },
                {
                    data: 'shopName',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'shopAdress',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'arreaManagementPerson.name',
                    render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'signPerson'
                },
                {
                    data: 'managementPerson.name'
                }
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();
                    if (target.className.indexOf('fa-download') != -1 || target.className.indexOf('downloadBtnE') != -1) {
                    	downloadQ(data);
                    }
                    
                    if (target.className.indexOf('fa-search') != -1 || target.className.indexOf('searchBtn') != -1) {
                        showDetail(data);
                    }

                    return;
                });
            }
        });
        	
    });
	
    //未绑定 全选
    $("#unbindingSelectAll").click(function() {
    		var unbinding = $(":checkbox[group=unbindingCB]");
        if ($(this).prop("checked")) {
        		unbinding.each(function() {
                $(this).prop("checked", true);  
            });  
        } else {  
        		unbinding.each(function() {  
                $(this).prop("checked", false);  
            });  
        }  
    }); 
    
    //已绑定 全选
    $("#bindingSelectAll").click(function() {
	    	var binding = $(":checkbox[group=bindingCB]");
	    	if ($(this).prop("checked")) {
	    		binding.each(function() {
	    			$(this).prop("checked", true);  
	    		});  
	    	} else {  
	    		binding.each(function() {  
	    			$(this).prop("checked", false);  
	    		});  
	    	}  
    }); 
    
    
    jQuery('.addBtn').on('click', function() {
    		var infoid = getUnBindingCB();
		var bussinessLevelId = jQuery('#bussinessLevelId').val();
		if(''==bussinessLevelId){
			alert('请选择商户区域！');
			return;
		}
		var api = window.rootPath+ "/ui/bussinessLevelBinding/binding";
		var d = {
			"bussinessLevelId" : bussinessLevelId ,
			"infoid" : infoid
		};
		jQuery.ajax({
					type : 'post',
					url : api,
					data : JSON.stringify(d),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function(data) {
						$("#unbindingSelectAll").prop("checked", false); 
						alert(data.msg);
						serverTable.draw(false);
						serverTable2.draw(false);
					}
				});
	});
    
    function getUnBindingCB(){
	    	var obj = $(":checkbox[group=unbindingCB]:checked");
	    	var myArray = new Array();
	    	var i =0;
	    	obj.each(function() {
	    		myArray[i++] = $(this).attr('infoid');
        });
	    	return myArray;
    }
    
    jQuery('.removeBtn').on('click', function() {
	    	var infoid = getBindingCB();
	    	var bussinessLevelId = jQuery('#bussinessLevelId').val();
	    	if(''==bussinessLevelId){
	    		alert('请选择商户区域！');
	    		return;
	    	}
	    	var api = window.rootPath+ "/ui/bussinessLevelBinding/unbinding";
	    	var d = {
	    			"bussinessLevelId" : bussinessLevelId ,
	    			"infoid" : infoid
	    	};
	    	jQuery.ajax({
	    		type : 'post',
	    		url : api,
	    		data : JSON.stringify(d),
	    		dataType : 'json',
	    		contentType : 'application/json;charset=utf-8',
	    		success : function(data) {
	    			$("#bindingSelectAll").prop("checked", false); 
	    			alert(data.msg);
	    			serverTable.draw(false);
	    			serverTable2.draw(false);
	    		}
	    	});
    });
    
    function getBindingCB(){
	    	var obj = $(":checkbox[group=bindingCB]:checked");
	    	var myArray = new Array();
	    	var i =0;
	    	obj.each(function() {
	    		myArray[i++] = $(this).attr('infoid');
	    	});
	    	return myArray;
    }
    
////////////////初始化模型的树结构/////////////////////////
    var modalSetting = {
        edit:{
            enable: true,
            showRemoveBtn:false,
            showRenameBtn:false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootParentId: 0
            }
        },
        view:{
            showLine:true
        },
        check: {
			enable: false,
			chkStyle: "radio",
			radioType: "all"
		},
        callback:{
        	onClick:editNewModule
        }

    };
    
    //菜单树点击节点
    function editNewModule(e, treeId, treeNode){
	    	var bussinessLevelId=treeNode.id;
	    	jQuery('#bussinessLevelId').val(bussinessLevelId);
	    	if(bussinessLevelId != '0'){
	    		jQuery('#bussinessLevelId').val(bussinessLevelId);
	    		//setUEContent(bussinessLevelId);
	    		getbusinesssellerBaseInfo(bussinessLevelId);
	    	}
    }
    
    function getbusinesssellerBaseInfo(bussinessLevelId){
    		serverData2.bussinessLevelId = bussinessLevelId;
    		serverTable2.draw(false);
    }
	
})();